/**
 * @(#)com.tansun.easycare.rule.excel.handle.impl.RuleDatasetHandle.java Copyright (c) 2017
 * 天阳宏业科技股份有限公司
 */
package com.tansun.easycare.rule.excel.handle.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.tansun.easycare.rule.datamodel.entity.RuleDataDomain;
import com.tansun.easycare.rule.excel.entity.ruledataset.RuleDataParamsExcelEntity;
import com.tansun.easycare.rule.excel.entity.ruledataset.RuleDatasetExcelEntity;
import com.tansun.easycare.rule.excel.handle.BaseHandle;
import com.tansun.easycare.rule.excel.utils.ExcelUitl;
import com.tansun.rule.utils.StringUtils;

/**
 *
 * @author zengdong
 * @version 1.0  2017年5月22日
 * @modified zengdong  2017年5月22日  <创建>
 */
public class RuleDatasetHandle extends BaseHandle<RuleDatasetExcelEntity> {

    @Override
    public List<RuleDatasetExcelEntity> excelToEntity(Workbook wb, String projectCode,String userId) {
        Date date = new Date();
        Row row = null;
        // 1.解析class表
        Sheet sheet = wb.getSheetAt(0);
        RuleDatasetExcelEntity dataset = null;
        //解析出的class记录集
        List<RuleDatasetExcelEntity> ruleDatasetList = new ArrayList<RuleDatasetExcelEntity>();
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);
            //若该行无信息则直接结束
            if (row == null) {
                break;
            }
            dataset = new RuleDatasetExcelEntity();
            dataset.setName(ExcelUitl.getStringCellValue(row.getCell(0)));
            dataset.setParamType(ExcelUitl.getStringCellValue(row.getCell(1)));
            dataset.setChzn(ExcelUitl.getStringCellValue(row.getCell(2)));
            dataset.setDataDesc(ExcelUitl.getStringCellValue(row.getCell(3)));
            dataset.setProjectcode(projectCode);
            ExcelUitl.dataEntitySet(dataset, date, userId);

            ruleDatasetList.add(dataset);
        }

        // 2.解析field表
        sheet = wb.getSheetAt(1);
        RuleDataParamsExcelEntity params = null;
        List<RuleDataParamsExcelEntity> ruleDataparamsList = new ArrayList<RuleDataParamsExcelEntity>();
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);
            //若该行无信息则直接结束
            if (row == null) {
                break;
            }
            params = new RuleDataParamsExcelEntity();
            String id = ExcelUitl.getStringCellValue(row.getCell(0));
            if (StringUtils.isEmpty(id)) {
                id = StringUtils.generateShortUuid();
            }
            params.setId(id);
            params.setProjectcode(projectCode);
            params.setDatasetId(ExcelUitl.getStringCellValue(row.getCell(1)));
            params.setName(ExcelUitl.getStringCellValue(row.getCell(2)));
            params.setType(ExcelUitl.getStringCellValue(row.getCell(3)));
            params.setChzn(ExcelUitl.getStringCellValue(row.getCell(4)));
            params.setParamDesc(ExcelUitl.getStringCellValue(row.getCell(5)));
            params.setDomain(ExcelUitl.getStringCellValue(row.getCell(6)));
            params.setDomainStr(ExcelUitl.getStringCellValue(row.getCell(7)));
            params.setCollection(ExcelUitl.getStringCellValue(row.getCell(8)));
            params.setIsout(ExcelUitl.getStringCellValue(row.getCell(9)));
            params.setPattern(ExcelUitl.getStringCellValue(row.getCell(10)));
            params.setMathStr(ExcelUitl.getStringCellValue(row.getCell(11)));
            params.setDefaultValue(ExcelUitl.getStringCellValue(row.getCell(12)));
            params.setIsDerives(ExcelUitl.getStringCellValue(row.getCell(13)));
            params.setIsCollections(ExcelUitl.getStringCellValue(row.getCell(14)));
            params.setDomainId("");
            ExcelUitl.dataEntitySet(params, date, userId);

            if (StringUtils.isNotBlank(params.getCollection())) {
                String collection = ExcelUitl.conversionStr(params.getCollection());
                params.setCollection(collection);
            }
            ruleDataparamsList.add(params);
        }

        // 3.解析domain表
        sheet = wb.getSheetAt(2);
        RuleDataDomain domain = null;
        List<RuleDataDomain> ruleDatadomain = new ArrayList<RuleDataDomain>();
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);
            //若该行无信息则直接结束
            if (row == null) {
                break;
            }
            domain = new RuleDataDomain();
            String id = ExcelUitl.getStringCellValue(row.getCell(0));
            if (StringUtils.isEmpty(id)) {
                id = StringUtils.generateShortUuid();
            }
            domain.setId(id);
            domain.setDomainId(ExcelUitl.getStringCellValue(row.getCell(1)));
            domain.setName(ExcelUitl.getStringCellValue(row.getCell(4)));
            domain.setDomainValue(ExcelUitl.getStringCellValue(row.getCell(5)));
            ruleDatadomain.add(domain);
        }
        if (ruleDataparamsList != null && ruleDataparamsList.size() > 0) {
            ruleDataparamsList.get(0).setRuledomainList(ruleDatadomain);
        }

        if (ruleDatasetList != null && ruleDatasetList.size() > 0) {
            ruleDatasetList.get(0).setParamsList(ruleDataparamsList);
        }

        return ruleDatasetList;
    }

    @Override
    public Workbook entityToExcel(List<RuleDatasetExcelEntity> list) {
        // 声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 设置这些样式
        HSSFCellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 设置字体
        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        style.setFont(font);

        // 生成class表
        HSSFSheet sheet = workbook.createSheet("class");
        // 产生表格标题行
        HSSFRow row = sheet.createRow(0);
        String headers[] = {"name", "paramType", "chzn", "desc"};
        int i = 0;
        for (i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(style);
            cell.setCellValue(new HSSFRichTextString(headers[i]));
        }

        //获取数据集对象列表
        List<RuleDataParamsExcelEntity> paramsList = new ArrayList<RuleDataParamsExcelEntity>();
        //数据集名称缓存
        Map<String, String> map = new HashMap<String, String>();
        i = 1;
        if (list != null && list.size() > 0) {
            for (RuleDatasetExcelEntity dataSet : list) {
                map.put(dataSet.getId(), dataSet.getName());
                row = sheet.createRow(i++);
                HSSFCell cell = row.createCell(0);
                cell.setCellValue(dataSet.getName());
                cell = row.createCell(1);
                cell.setCellValue(dataSet.getParamType());
                cell = row.createCell(2);
                cell.setCellValue(dataSet.getChzn());
                cell = row.createCell(3);
                cell.setCellValue(dataSet.getDataDesc());
                if (dataSet.getParamsList() != null && dataSet.getParamsList().size() > 0) {
                    paramsList.addAll(dataSet.getParamsList());
                }
            }
        }

        // 生成field表
        sheet = workbook.createSheet("field");
        row = sheet.createRow(0);
        headers = new String[]{"id", "class", "name", "type", "chzn", "desc", "domain", "domainStr", "collection",
                "isout", "pattern", "mathStr", "defaultValue", "isDerives", "isCollections"};
        for (i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(style);
            cell.setCellValue(new HSSFRichTextString(headers[i]));
        }
        i = 1;

        //获取数据集变量列表
        List<RuleDataDomain> domainList = new ArrayList<RuleDataDomain>();
        //构建查询参数
        if (paramsList != null && paramsList.size() > 0) {
            for (RuleDataParamsExcelEntity dataparam : paramsList) {
                row = sheet.createRow(i++);
                row.createCell(0).setCellValue(dataparam.getId());
                row.createCell(1).setCellValue(map.get(dataparam.getDatasetId()));
                row.createCell(2).setCellValue(dataparam.getName());
                row.createCell(3).setCellValue(dataparam.getType());
                row.createCell(4).setCellValue(dataparam.getChzn());
                row.createCell(5).setCellValue(dataparam.getParamDesc());
                row.createCell(6).setCellValue(dataparam.getDomain());
                row.createCell(7).setCellValue(dataparam.getDomainStr());
                row.createCell(8).setCellValue(dataparam.getCollection());
                row.createCell(9).setCellValue(dataparam.getIsout());
                row.createCell(10).setCellValue(dataparam.getPattern());
                row.createCell(11).setCellValue(dataparam.getMathStr());
                row.createCell(12).setCellValue(dataparam.getDefaultValue());
                row.createCell(13).setCellValue(dataparam.getIsDerives());
                row.createCell(14).setCellValue(dataparam.getIsCollections());
                if (dataparam.getRuledomainList() != null && dataparam.getRuledomainList().size() > 0) {
                    domainList.addAll(dataparam.getRuledomainList());
                }
            }
        }

        // 生成field表
        sheet = workbook.createSheet("domain");
        row = sheet.createRow(0);
        headers = new String[]{"id", "domain_id", "name", "domain_value"};
        for (i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(style);
            cell.setCellValue(new HSSFRichTextString(headers[i]));
        }
        i = 1;

        //获取离散型变量
        if (domainList != null && domainList.size() > 0) {
            for (RuleDataDomain dom : domainList) {
                row = sheet.createRow(i++);
                row.createCell(0).setCellValue(dom.getId());
                row.createCell(1).setCellValue(dom.getDomainId());
                row.createCell(2).setCellValue(dom.getName());
                row.createCell(3).setCellValue(dom.getDomainValue());
            }
        }
        return workbook;
    }

}
